import 'package:flutter/material.dart';
import 'package:flutter_provider_demo/config/dimens.dart';
import 'package:flutter_provider_demo/ui/cart/cart_viewmodel.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class CartScreen extends StatelessWidget {
  const CartScreen({super.key, required this.viewModel});
  final CartViewModel viewModel;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          AppLocalizations.of(context)!.cart,
          style: Theme.of(context).textTheme.displayLarge!.copyWith(color: Theme.of(context).colorScheme.primary),
        ),
        elevation: 8.0,
        shadowColor: Theme.of(context).colorScheme.onSurface,
      ),
      body: Container(
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.secondary,
          border: Border(top: BorderSide(color: Theme.of(context).colorScheme.onSecondary, width: 2)),
        ),
        child: Column(
          children: [
            Expanded(
              flex: 5,
              child: _CartList(
                viewmodel: viewModel,
              ),
            ),
            Expanded(
              flex: 2,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    '${AppLocalizations.of(context)!.totalPrice}: ${viewModel.totalPrice}',
                    style: Theme.of(context).textTheme.displayLarge,
                  ),
                  SizedBox(width: 12.0),
                  ElevatedButton(
                    onPressed: () {},
                    style: ElevatedButton.styleFrom(
                      shape: RoundedRectangleBorder(
                        borderRadius: Dimens.buttonRadius,
                      ),
                    ),
                    child: Text(
                      AppLocalizations.of(context)!.buy,
                      style: Theme.of(context).textTheme.titleLarge!.copyWith(color: Theme.of(context).colorScheme.primary),
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

class _CartList extends StatelessWidget {
  const _CartList({
    required this.viewmodel,
  });
  final CartViewModel viewmodel;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(12.0),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Colors.black,
            width: 4,
          ),
        ),
      ),
      child: ListenableBuilder(
          listenable: viewmodel,
          builder: (context, _) {
            return ListView.builder(
              itemCount: viewmodel.carts.length,
              itemBuilder: (context, index) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    viewmodel.carts[index].catalogName,
                    style: Theme.of(context).textTheme.titleLarge,
                  ),
                );
              },
            );
          }),
    );
  }
}
